package cn.cvs.controller;

import cn.cvs.pojo.User;
import cn.cvs.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/")
    public  String loginX(HttpSession session) {
         return "login";
    }

//    @GetMapping("/login")
////    public  String login(HttpSession session){
////        User user = (User) session.getAttribute("user");
////        if(user != null){
////            session.removeAttribute("user");
////            return "zhuJieMian";
////        } else {
////            return  "login";
////        }
////    }

    @GetMapping("/doLogin")
    public  String slogin(HttpSession session){
        User user = (User) session.getAttribute("user");
        if(user != null){
            session.removeAttribute("user");
            return "login";
        }
        return  "login";
    }

    @PostMapping("/doLogin")
    public  String doLogin(@Param("uPhone")String uPhone, @Param("uPassword")String uPassword, HttpSession session, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        User user = userService.loginUserByUPhone(uPhone, uPassword);
            if(user!=null) {
                if(user.getuStatus()==0){
                    session.setAttribute("user", user);
                    return "redirect:/index";
                }else {
                    session.setAttribute("user", user);
                    return "add";
                }

            }else{
                    out.print("<script>alert('手机号或密码错误！');location.href='doLogin'</script>");
            }
            out.flush();
            return "login";
    }

    @ResponseBody
    @PostMapping("/check")
    public Object check(@Param("uPhone")String uPhone,HttpSession session, HttpServletResponse response){
        User user = userService.getUserByUPhone(uPhone);
        if(user==null){

            return false;
        }else{
            session.setAttribute("user",user);
            return  true;
        }
    }
    @GetMapping("/update")
    public String toUpdate(){
        return "update";
    }
    @PostMapping("/xiuGai")
    private String toXiuGai(@Param("uPhone") String uPhone,@Param("uPassword") String uPassword){
        System.out.println(uPhone);
        User userByUPhone = userService.getUserByUPhone(uPhone);
        System.out.println(userByUPhone);
        System.out.println(uPassword);
        userByUPhone.setuPassword(uPassword);
        userService.updatePassWord(userByUPhone);
        return "redirect:/";
    }
    @GetMapping("/reg")
    public String reg(){

        return "reg";
    }

    @ResponseBody
    @PostMapping("/checkreg")
    public Object regYangZhe(@RequestParam("uPhone")String uPhone){
        User userByUPhone = userService.getUserByUPhone(uPhone);
        System.out.println(userByUPhone.toString());
        if(userByUPhone!=null){
            return true;
        }
        return false;
    }
   @ResponseBody
    @PostMapping("/add")
    public Object add(@RequestParam("uName") String uName,@RequestParam("uCardId")String uCardId,@RequestParam("uPhone")String uPhone,@RequestParam("uPassword")String uPassword){
        int uHtype=1;
        int uStatus=0;
        String uGender=getSex(uCardId);
        User user=new User();
        user.setuName(uName);
        user.setuCardId(uCardId);
        user.setuPhone(uPhone);
        user.setuGender(uGender);
        user.setuPassword(uPassword);
        user.setuHtype(uHtype);
        user.setuStatus(uStatus);
        int count = userService.addUser(user);
        return count;
    }
    public String getSex(String cardId){
        String substring = cardId.substring(16, 17);
        int i = Integer.parseInt(substring);
        if(i%2==0){
            return "1";
        }else {
            return "0";
        }
    }
}